package TCP;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;


public class Day04_OpenURLCapturePicture {
    /**
     * main 方法是程序的入口点。
     *
     * @param args 命令行参数（在此示例中未使用）
     */
    public static void main(String[] args) {
        String image ="https://www.bing.com/images/search?view=detailV2&ccid=8sRBqy4r&id=12E7747A5FEBB1C1A49100A072435A7BC905C206&thid=OIP.8sRBqy4rxjr9sBgsPpNlowHaLH&mediaurl=https%3a%2f%2ffile.moyublog.com%2fd%2ffile%2f2021-02-21%2f751d49d91fe63a565dff18b3b97ca7c8.jpg&exph=1800&expw=1200&q=%e5%9b%be%e7%89%87&simid=608039100586932601&FORM=IRPRST&ck=7805CFF74BF866DC6DADD7BC5641632A&selectedIndex=9&itb=0&ajaxhist=0&ajaxserp=0";     //2.创建图片链接
        try {
            URL url = new URL(image);
            //3.使用URL的openStream方法转换为流的形式
            InputStream inputStream = url.openStream();
            //4.确定图片存储的位置(输出流的形式走)
            //FileOutputStream用于写入诸如图像数据的原始字节流。 对于写入字符流，请考虑使用FileWriter
            FileOutputStream outputStream = new FileOutputStream("D://Downloads//image4.png");
            //5.读取inputStream里面的内容
            int c;
            while ((c = inputStream.read()) != -1) {
                //6.写入磁盘
                outputStream.write(c);

            }
            inputStream.close();
            outputStream.close();

        } catch (MalformedURLException e) {

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
